﻿using RealTimeCharts.LineChart;
using RealTimeCharts.LineChart.common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;

namespace WPFCustomControls.views
{
    /// <summary>
    /// RealTimeChartPage.xaml 的交互逻辑
    /// </summary>
    public partial class RealTimeChartPage : Page
    {
        private LineChart lineChart;
        List<System.Drawing.Color> chSeriesColor;

        public RealTimeChartPage()
        {
            InitializeComponent();

            this.canvas.SizeChanged += new SizeChangedEventHandler(Canvas_SizeChange);

            chSeriesColor = MakeLegendColorTable();
        }

        private void Canvas_SizeChange(object sender, SizeChangedEventArgs e)
        {
            CreateChart(false);
            ShowData();
        }

        private void CreateChart(bool reset)
        {
            int width = (int)canvas.ActualWidth;
            int height = (int)canvas.ActualHeight;

            if (width > 0 && height > 0)
            {
                // 设置image的size
                chartImage.Width = width;
                chartImage.Height = height;
            }

            if (lineChart == null || reset)
            {

                // y 轴标签
                TitleProperty titlePropertyY = new TitleProperty();
                titlePropertyY.title = string.Format("温度（℃）");
                titlePropertyY.titleTextFont = new Font("Microsoft YaHei UI", 10, System.Drawing.FontStyle.Bold);
                AxisTitle axisTitle = new AxisTitle(null, titlePropertyY, (int)Position.Left);

                // 图表标题
                TitleProperty titleProperty = new TitleProperty();
                titleProperty.title = string.Format("温度");
                titleProperty.titleTextFont = new Font("Microsoft YaHei UI", 10, System.Drawing.FontStyle.Bold);
                Title title = new Title(titleProperty);

                AxisScaleWindow axisScaleWindow = new AxisScaleWindow();
                //axisScaleWindow.scalePos = (int)(Position.Left | Position.Bottom);

                axisScaleWindow.scaleX.mode = SCALE_SHOW_MODE.Normal;
                axisScaleWindow.scaleX.valueRange.minValue = 0;
                axisScaleWindow.scaleX.valueRange.maxValue = 1000;
                axisScaleWindow.scaleX.scaleTextFormat = "0:F0";
                axisScaleWindow.scaleX.scaleTextFont = new Font("Microsoft YaHei UI", 10, System.Drawing.FontStyle.Regular);
                ScaleLineProperty linePropertyX = new ScaleLineProperty();
                linePropertyX.color = System.Drawing.Color.LightGray;
                linePropertyX.width = 1;
                linePropertyX.type = LineType.Solid;
                axisScaleWindow.scaleX.lineProperty = linePropertyX;

                axisScaleWindow.scaleY.mode = SCALE_SHOW_MODE.Readable;
                axisScaleWindow.scaleY.valueRange.minValue = -1;
                axisScaleWindow.scaleY.valueRange.maxValue = 1;
                axisScaleWindow.scaleY.scaleTextFormat = "0:F2";
                axisScaleWindow.scaleY.scaleTextFont = new Font("Microsoft YaHei UI", 10, System.Drawing.FontStyle.Regular);
                ScaleLineProperty linePropertyY = new ScaleLineProperty();
                linePropertyY.color = System.Drawing.Color.LightGray;
                linePropertyY.width = 1;
                linePropertyY.type = LineType.Solid;
                axisScaleWindow.scaleY.lineProperty = linePropertyY;

                axisScaleWindow.UpdateScaleValue();

                // 图例
                List<int> channelNoList = new List<int>();
                //for (int i = 1; i < 25; i++)
                //{
                    channelNoList.Add(1);
                //}
                System.Drawing.Pen borderPen = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
                ColorLegend colorLegend = MakeLegend(channelNoList, new Font("Arial", 8), borderPen);

                lineChart = new LineChart(width, height, System.Drawing.Color.White, title, axisTitle, axisScaleWindow, colorLegend);
                //lineChart = new LineChart(width, height, Color.FromArgb(0x00, 0xff, 0xff, 0xff), title, axisTitle, axisScaleWindow, null);
                lineChart.isColorLengendShow = false;

                lineChart.elemList = new List<List<ValuePoint>>();
                for (int i = 0; i < 25; i++)
                {
                    List<ValuePoint> newLine = new List<ValuePoint>();
                    lineChart.elemList.Add(newLine);
                }
            }
            else
            {
                lineChart.ReSize(width, height);
            }

            chartImage.Source = lineChart.GetSource();

            lineChart.Clear();
            lineChart.Show(true);
        }

        private void ShowData()
        {
            List<List<ValuePoint>> list = new List<List<ValuePoint>>();
            List<ValuePoint> itemList = new List<ValuePoint>();
            for(int i = 0; i < 1000; i++)
            {
                ValuePoint point = new ValuePoint();
                point.valX = i;
                point.valY = Math.Sin((i * Math.PI) / 180);
                itemList.Add(point);
            }
            list.Add(itemList);
            lineChart.elemList = list;
            lineChart.Show(true);
        }
        private List<System.Drawing.Color> MakeLegendColorTable()
        {
            // 浅色背景
            List<System.Drawing.Color> colorList = new List<System.Drawing.Color>{
            Color.Black, // 1
            Color.Red, // 2
            Color.Green, // 3
            Color.Blue, // 4
            Color.Yellow, // 5
            Color.Orange, // 6
            Color.Purple, // 7
            Color.Pink, // 8
            Color.Brown, // 9
            Color.Gray, // 10
            Color.DarkRed, // 11
            Color.DarkGreen, // 12
            Color.DarkBlue, // 13
            Color.DarkOrange, // 14
            Color.DarkViolet, // 15
            Color.DeepPink, // 16
            Color.DarkCyan, // 17
            Color.DarkOliveGreen, // 18
            Color.Chocolate, // 19
            Color.SaddleBrown, // 20
            Color.Sienna, // 21
            Color.Maroon, // 22
            Color.ForestGreen, // 23
            Color.Navy, // 24
            Color.RoyalBlue, // 25
            Color.Indigo, // 26
            Color.Crimson, // 27
            Color.DarkSlateBlue, // 28
            Color.DarkMagenta, // 29
            Color.Teal, // 30
            Color.DarkTurquoise, // 31
            Color.MidnightBlue // 32
        };
            // 深色背景
            //List<System.Drawing.Color> colorList = new List<System.Drawing.Color>();
            //colorList.Add(System.Drawing.Color.FromArgb(0xFF, 0x00, 0x00));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0xFF, 0x00));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0x00, 0xFF));
            //colorList.Add(System.Drawing.Color.FromArgb(0xFF, 0xFF, 0x00));
            //colorList.Add(System.Drawing.Color.FromArgb(0xFF, 0x00, 0xFF));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0xFF, 0xFF));
            //colorList.Add(System.Drawing.Color.FromArgb(0x80, 0x00, 0x00));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0x80, 0x00));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0x00, 0x80));
            //colorList.Add(System.Drawing.Color.FromArgb(0x80, 0x80, 0x00));
            //colorList.Add(System.Drawing.Color.FromArgb(0x80, 0x00, 0x80));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0x80, 0x80));
            //colorList.Add(System.Drawing.Color.FromArgb(0xFF, 0xB0, 0xB0));
            //colorList.Add(System.Drawing.Color.FromArgb(0x80, 0x80, 0x80));
            //colorList.Add(System.Drawing.Color.FromArgb(0xFF, 0x40, 0x40));
            //colorList.Add(System.Drawing.Color.FromArgb(0xA0, 0x80, 0x20));
            //colorList.Add(System.Drawing.Color.FromArgb(0x20, 0x80, 0x20));
            //colorList.Add(System.Drawing.Color.FromArgb(0x32, 0xCD, 0x32));
            //colorList.Add(System.Drawing.Color.FromArgb(0x48, 0xD1, 0xCC));
            //colorList.Add(System.Drawing.Color.FromArgb(0x64, 0x95, 0xED));
            //colorList.Add(System.Drawing.Color.FromArgb(0x41, 0x69, 0xE1));
            //colorList.Add(System.Drawing.Color.FromArgb(0xEE, 0x82, 0xEE));
            //colorList.Add(System.Drawing.Color.FromArgb(0x8A, 0x2B, 0xE2));
            //colorList.Add(System.Drawing.Color.FromArgb(0xDB, 0x70, 0x93));
            //colorList.Add(System.Drawing.Color.FromArgb(0x00, 0x00, 0x00));

            return colorList;
        }

        private ColorLegend MakeLegend(List<int> channelNoList, Font font, System.Drawing.Pen borderPen)
        {
            List<ColorLegendData> colorLegendDataList = new List<ColorLegendData>();
            foreach (var v in channelNoList)
            {
                ColorLegendData newData = new ColorLegendData();
                newData.color = chSeriesColor[v - 1];
                newData.text = "通道" + v.ToString();
                colorLegendDataList.Add(newData);
            }
            ColorLegend colorLegend = new ColorLegend(colorLegendDataList, font, borderPen, Position.TopRight);
            return colorLegend;
        }
    }
}
